//
//  Contacts.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-16.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "Contact.h"
#import "Department.h"
#import "Company.h"
#import "ContactDao.h"
#import "FileInterface.h"
@implementation Contact
@synthesize name,loginName,phone,mobilephone,department,officeNo,userID,isActivity,headUrl,company,token,position,password,headName,namePinyin,mark;
+(Contact *) sharedContact{
    static Contact *contact = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        contact = [[Contact alloc]init];
//        contact.userID = dic[@"id"];
//        contact.name = dic[@"name"];
//        contact.phone = dic[@"phone"];
//        contact.mobilephone = dic[@"mobile"];
//        contact.officeNo =  dic[@"officeNo"];
//        contact.isActivity = ![dic[@"isFirstLogin"]boolValue];
//        contact.token =dic[@"token"];
//        
//        Department *department =[[Department alloc]init];
//        department.name =dic[@"departmentName"];
//        department.ID = dic[@"departmentID"];
//        contact.department = department;
//        
//        Company *company = [[Company alloc]init];
//        company.ID = dic[@"companyId"];
//        company.name =dic[@"companyName"];
//        contact.company = company;
    });
    
    return contact;
}
+(Contact *)analysisContactWithDic:(NSDictionary *)dic{
    Contact *contact = [[Contact alloc]init];
    contact.userID = [dic[@"id"]integerValue];
    contact.name = dic[@"name"];
    contact.phone = dic[@"phone"];
    contact.mobilephone = dic[@"mobile"];
    contact.officeNo =  dic[@"officeNo"];
    contact.isActivity  = [dic[@"isFirstLogin"] isEqualToString:@"1"]?(NO):(YES);
    contact.headUrl = dic[@"headUrl"];
    if ([[contact.headUrl lastPathComponent]hasSuffix:@"png"]||[[contact.headUrl lastPathComponent]hasSuffix:@"jpg"]) {
        contact.headName =[contact.headUrl lastPathComponent];
    }
    contact.namePinyin = dic[@"namePinyin"];
    contact.position = dic[@"position"];
    contact.mark = dic[@"mark"];

    return contact;
}

+(Contact *)analysisUserInfoDic:(NSDictionary *)dic{
    Contact *contact = [[Contact alloc]init];
    contact.userID = [dic[@"id"]integerValue];
    contact.name = dic[@"name"];
    contact.phone = dic[@"phone"];
    contact.mobilephone = dic[@"mobile"];
    contact.officeNo =  dic[@"officeNo"];
    contact.isActivity  = [dic[@"isFirstLogin"] isEqualToString:@"1"]?(NO):(YES);
    contact.token =dic[@"token"];
    contact.headUrl = dic[@"headUrl"];
    if ([[contact.headUrl lastPathComponent]hasSuffix:@"png"]||[[contact.headUrl lastPathComponent]hasSuffix:@"jpg"]) {
        contact.headName =[contact.headUrl lastPathComponent];
    }
    contact.namePinyin = dic[@"namePinyin"];
    contact.position = dic[@"position"];
    contact.mark = dic[@"mark"];

    
    
    Department *department =[[Department alloc]init];
    department.name =dic[@"departmentName"];
    department.ID = [dic[@"departmentID"]integerValue];
    contact.department = department;
    
    Company *company = [[Company alloc]init];
    company.ID = dic[@"companyId"];
    company.name =dic[@"companyName"];
    company.memberCount=[dic[@"memberCount"] integerValue];
    contact.company = company;
    
    return contact;
}

+(NSArray *)departmentContacts:(NSInteger)departmentID{
    ContactDao *cDao =[[ContactDao alloc]init];
    NSArray *contacts  = [cDao getContactsWithDepartmentID:departmentID];
    return contacts;
}
+(NSArray *)lastContacts{
    NSMutableArray *arr = nil ;
    //NSDictionary *dic = [FileInterface getLastContacts];
    NSArray *contacts = [FileInterface getLastContacts];
    ContactDao *cDao =[[ContactDao alloc]init];
    for (NSInteger i = contacts.count -1;i>=0; i-- ) {
        NSString *contactID = contacts[i];
        if (!arr) {
            arr = [[NSMutableArray alloc]initWithCapacity:contacts.count];
        }
        Contact *contact = [cDao getContactWithUserID:[contactID integerValue]];
        [arr addObject:contact];
        
    }
    return arr;
}

+(BOOL)updateContact:(Contact *)contact{
    ContactDao *cDao =[[ContactDao alloc]init];
    BOOL success = [cDao updateContact:contact];
    return success;
}
+(BOOL)saveContacts:(NSArray *)contacts{
    ContactDao *cDao =[[ContactDao alloc]init];
    BOOL success = [cDao insertContacts:contacts];
    return success;
}
+(BOOL)updateContacts:(NSArray *)contacts{
    ContactDao *cDao =[[ContactDao alloc]init];
    BOOL success = [cDao updateContacts:contacts];
    return success;
}

+(BOOL)deleteContact:(Contact *)contact{
    ContactDao *cDao =[[ContactDao alloc]init];
    BOOL success = [cDao deleteContact:contact];
    return success;
}

+(BOOL)saveContact:(Contact *)contact{
    ContactDao *cDao =[[ContactDao alloc]init];
    BOOL success = [cDao insertContact:contact];
    return success;
}

+(NSArray *)searchContactsWithKey:(NSString *)key{
    ContactDao *cDao =[[ContactDao alloc]init];
    NSArray *contacts  = [cDao searchContactsWithKey:key];
    return contacts;
}
@end
